package com.syqc.monitor.ui.repair

import autodispose2.autoDispose
import com.luck.picture.lib.entity.LocalMedia
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import com.syqc.utils.ToolsUtil
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.BehaviorSubject
import okhttp3.MultipartBody
import kotlin.concurrent.thread

/**
 *@Author Jasper
 *@Time   2021/1/14
 *@Desc   维修申报
 */
class SubmitRepairViewModel : AutoDisposeViewModel() {
    var carId = ""
    var repairName = ""
    var repairPhone = ""
    var repairDesc = ""
    private val maxNum = 6
    var nowNum = maxNum
    private val photoSub by lazy { BehaviorSubject.create<ArrayList<String>>() }
    fun photosObserver(): Observable<ArrayList<String>> = photoSub.hide()

    private val repairPhotos = arrayListOf<String>("", "", "", "", "", "")
    private var emptySize = 0


    fun handlerPhoto(resultPhotos: ArrayList<LocalMedia>?) {
        if (resultPhotos.isNullOrEmpty()) {
            photoSub.onNext(repairPhotos)
            return
        }
        for (photo in resultPhotos) {
            repairPhotos[emptySize] = photo.path
            emptySize++
        }
        nowNum = maxNum - emptySize

        photoSub.onNext(repairPhotos)
    }


    /**
     * 移除一个
     */
    fun removePhoto(index: Int) {
        val photos = arrayListOf<String>()
        var size = 0
        repairPhotos[index] = ""
        for (photo in repairPhotos) {
            if (photo.isNotEmpty()) {
                photos.add(photo)
                size++
            }
        }
        emptySize = size
        nowNum = maxNum - emptySize
        if (size < maxNum) {
            for (f in 0 until nowNum) {
                photos.add("")
            }
        }
        repairPhotos.clear()
        repairPhotos.addAll(photos)
        photoSub.onNext(repairPhotos)
    }

    private fun getBodyPartsByPath(): ArrayList<MultipartBody.Part?>? {
        if (repairPhotos.isEmpty()) return null
        val parts = ArrayList<MultipartBody.Part?>()
        for ((index, path) in repairPhotos.withIndex()) {
            if (path.isNotEmpty()) parts.add(
                ToolsUtil.getPartByPath("file", "repair_pic_$index", path)
            )
        }
        return parts
    }


    private var isUploading = false
    fun submitRepair() {
        if (isUploading) return
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }
        if (repairName.isEmpty()) {
            toast(R.string.hint_repair_name)
            return
        }
        if (repairPhone.isEmpty()) {
            toast(R.string.desc_repair_phone)
            return
        }
        if (repairDesc.isEmpty()) {
            toast(R.string.hint_toast_desc)
            return
        }
        val data = "$carId$repairName$repairPhone$repairDesc$repairPhotos"
        if (oldData == data) {
            toast(R.string.toast_repeat_same_data)
            return
        }
        toastLoading(true)
        isUploading = true
        thread {
            val parts = getBodyPartsByPath()
            ServiceHelper.getRepository()
                .submitRepair(carId, repairName, repairPhone, repairDesc, parts)
                .autoDispose(this).subscribe({
                    oldData = "$carId$repairName$repairPhone$repairDesc$repairPhotos"
                    toastLoading(false)
                    toast(R.string.toast_add_success_repair)
                    isUploading = false
                }, {
                    isUploading = false
                    toastLoading(false)
                    it.message?.run { toast(this) }
                })
        }
    }


    private var oldData = ""

}
